/**
 * Created by admin on 2017/5/3.
 */
(function ($, document) {
    /*------------------初始化字段-------------------------------*/
    var contact1 = "";
    var contact_relation1 = "";
    /*-----------------初始化PopPicker对象-----------------------*/
    var contact = new $.PopPicker();
    var contact_relation = new $.PopPicker();
    /*------------------给初始化PopPicker对象赋值----------------*/
    contact.setData([
        {
            value: 1,
            text: '配偶'
        }, {
            value: 2,
            text: '父母'
        }, {
            value: 3,
            text: '兄弟'
        }, {
            value: 4,
            text: '姐妹'
        }, {
            value: 5,
            text: '子女'
        }
    ]);
    contact_relation.setData([
        {
            value: 6,
            text: '同学'
        }, {
            value: 7,
            text: '亲戚'
        }, {
            value: 8,
            text: '同事'
        }, {
            value: 9,
            text: '朋友'
        }, {
            value: 10,
            text: '其他'
        }
    ]);
    /*-------------------------定义公共方法(绑定数据)----------------------------*/
    function pickevent(mm, nn, cc, dd) {
        document.getElementById(mm).addEventListener('tap', function () {
            if (mm.indexOf("Time") > 0) {
                nn.show(function (e) {
                    document.getElementById(dd).innerText = e.text;
                });
            } else {
                nn.show(function (e) {
                    document.getElementById(dd).innerText = e[0].text;
                    cc = e[0].value;
                    console.log(cc);
                    document.getElementById(mm).style.color = 'black';
                    sessionStorage.setItem(dd, cc);
                });
            }
        }, false);
    }

    function pickeventcity(mm, nn, cc, dd) {
        document.getElementById(mm).addEventListener('tap', function () {
            nn.show(function (items) {
                document.getElementById(cc).innerText = items[0].text + items[1].text;
                cc = items[0].text + items[1].text;
                var aaa = items[1].value;
                console.log(cc);
                sessionStorage.setItem(dd, cc);
                sessionStorage.setItem(dd + "_code", aaa);
                document.getElementById(mm).style.color = 'black';
                //返回 false 可以阻止选择框的关闭
                //return false;
            });
        }, false);
    }
    /*------------------------绑定pickevent----------------------------*/
    pickevent('contact', contact, contact1, "contact1");//直系联系人
    pickevent('contactRelation', contact_relation, contact_relation1, "contact_relation1");//其他联系人
})(mui, document);


$(function () {

    var header = tool.header;
    var consume_time = Date.parse(new Date());
    header.public_key = localStorage.getItem("public_key");//获取公钥
    header.app_key = localStorage.getItem("app_key");//获取app标识
    header.source = localStorage.getItem("source");//用户来源
    header.client_id = localStorage.getItem("client_id");//用户手机唯一标识
    header.channel = localStorage.getItem("channel");//打包渠道
    header.app_version = localStorage.getItem("app_version");//APP版本号
/*------------------------加载时获取服务器数据----------------------------------------*/
    function OnLoadInfo() {
        var flag = util.GetQueryString("flag");
        if (flag == 2) {

            var data = {
                user_id: util.GetQueryString("user_id")
            };
            var paramsStr = getparamsStr(data);
            var signaturesign = header.public_key + header.app_key + header.source + header.channel + header.client_id + header.app_version + paramsStr + header.timestamp;
            //签名信息MD5加密
            var signaturesignature = hex_md5(signaturesign);
            header.signature = signaturesignature;
            var urls = tool.httpsUrl + "";
            tool.ajax(urls, header, data, function (data) {

                console.log(data);
            });
        }
    }
    OnLoadInfo();

    /*-----------------------------------提交按钮---------------------------------------------------*/
    $('#contactInfoSubmit').click(function () {
        //定义后台传参
        var data = {
            consume_time: '',
            userUrgentInfoList: '',
            user_id: ''
        };
        //定义过渡对象-------------------获取所有联系人信息
        var userUrgentInfoList = [
            {
                contact_address: '',
                contact_relation: '',
                contact_name: '',
                contact_type: '1',
                contact_phone: ''
            }, {
                /*           contact_address: '',*/
                contact_relation: '',
                contact_name: '',
                contact_type: '2',
                contact_phone: ''
            }];
        //定义过渡对象-----------------获取所有联系人信息(赋值)
        userUrgentInfoList[0].contact_address = $("#emergencyContactAddress").val();
        userUrgentInfoList[0].contact_relation = sessionStorage.getItem("contact1");
        userUrgentInfoList[0].contact_name = $("#emergencyContactName").val();
        userUrgentInfoList[0].contact_phone = $("#emergencyContactPhone").val();
        userUrgentInfoList[1].contact_relation = sessionStorage.getItem("contact_relation1");
        userUrgentInfoList[1].contact_name = $("#otherContactName").val();
        userUrgentInfoList[1].contact_phone = $("#otherContactPhone").val();
        /*        userUrgentInfoList[1].contact_address = $("#otherContactPhoneAddress").val();*/
        //判断是否为空
        for (var i in data) {
            var info = data[i];
            for (var j in info) {
                if (info[j] == "") {
                    mui.alert("请完整填写联系人信息！", '提示', function () {
                        console.log(3);
                    });
                    throw"1";
                }
            }
        }
        //判断联系人号码时候正确
        if (util.phone(userUrgentInfoList[0].contact_phone) == "1") {
            if (util.phone(userUrgentInfoList[1].contact_phone) == "1") {
                console.log("电话号码输入正确");
            } else {
                mui.alert("请正确填写其他联系人电话号码!", '提示', function () {
                    console.log(3);
                });
                throw"1";
            }
        }
        else {
            mui.alert("请正确填写直系亲属联系人电话号码!", '提示', function () {
                console.log(3);
            });
            throw "1";
        }
        //用户id
        data.user_id = localStorage.getItem("userID"); //852437767772422144 1493266469550
        //联系人信息转字符串
        /*      data.userUrgentInfoList=userInfo;*/
        //填写联系人信息所用时间
        data.consume_time = new Date().getTime() - consume_time;
        data.userUrgentInfoList = JSON.stringify(userUrgentInfoList);
        var paramsStr = getencodeURIComponent(data);
        var signaturesignature = "";//签名
        var signaturesign = "";
        //填写联系人信息签名信息
        header.timestamp = new Date().getTime();
        signaturesign = header.public_key + header.app_key + header.source + header.channel + header.client_id + header.app_version + paramsStr + header.timestamp;
        //签名信息MD5加密
        signaturesignature = hex_md5(signaturesign);
        header.signature = signaturesignature;
        var url = tool.httpsUrl + "/archives/postUserUrgentInfo";
        tool.ajax(url, header, data, function (data) {
            localStorage.setItem("contactInfoSubmit", true);
            if (data.success) {

                window.location.href = "payroll_in_information.html";
            }

        });
    });


});